package com.infolab.aims.domain;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;


import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;

import org.hibernate.validator.constraints.NotBlank;

import com.infolab.aims.common.SpecialMuLineToStringStyle;
import com.infolab.aims.common.SpecialToStringStyle;
import com.infolab.aims.common.bean.AbstractModelBean;


@Entity
@Table(name="AMTB_SYSTEM_ROLE")
public class SystemRole extends AbstractModelBean implements Serializable {
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer systemRoleId;
	
	@NotBlank(message="{error.empty}")
	private String roleName;
	
	@ManyToOne(fetch=FetchType.EAGER)
	@JoinColumn(name="sectionId")
	@NotNull(message="{error.empty}")
	private Section section;
	
	@ManyToMany(fetch=FetchType.EAGER)
	@JoinTable(name="AMTB_SYSTEM_ROLE_ACCESSRIGHT",
	joinColumns=@JoinColumn(name="systemRoleId"),
	inverseJoinColumns=@JoinColumn(name="accessRightId"))
	private List<SystemAccessRight> systemAccessRights;
	
	@Column(columnDefinition="number(1,0) default 1 not null")
	private boolean active;

	public Integer getSystemRoleId() {
		return systemRoleId;
	}

	public void setSystemRoleId(Integer systemRoleId) {
		this.systemRoleId = systemRoleId;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public Section getSection() {
		return section;
	}

	public void setSection(Section section) {
		this.section = section;
	}

	public List<SystemAccessRight> getSystemAccessRights() {
		return systemAccessRights;
	}

	public void setSystemAccessRights(List<SystemAccessRight> systemAccessRights) {
		this.systemAccessRights = systemAccessRights;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}
	
	public String getAuditContentHead()
	{
		
		Section sec = section!=null? section: new Section();
				
		return new ToStringBuilder(this,SpecialToStringStyle.SPECIAL_STYLE)
			.append("Role Name",roleName)
			.append("Section ", sec.getSectionName())
			.toString();
	}
	
	public String getAuditContentDetail()
	{
		ToStringBuilder builder = new ToStringBuilder(this,SpecialMuLineToStringStyle.SPECIAL_MULINE_STYLE);
		if(systemAccessRights!=null && CollectionUtils.isNotEmpty(systemAccessRights))
		{
			int i = 0;
			for(SystemAccessRight systemAccessRight : systemAccessRights)
			{
				if(systemAccessRight.getAccessRightId()!= null){
					builder.append("  " + (++i), systemAccessRight.getAuditContentHead());
				}
			}
			String str = builder.toString();
			return StringUtils.substring(str, str.indexOf("[")+1, str.lastIndexOf("]")-1);
		}
		
		
		return super.getAuditContentDetail();
	}

	
	public String getAuditCode()
	{
		if(StringUtils.isNotEmpty(roleName)){
			return roleName;
		}
		return super.getAuditCode();
		
	}
	
	
	
}

